package com.tsy.pointcloud.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileMD5Calculator {

    public static String calculateFileMD5(File file) throws IOException, NoSuchAlgorithmException {
        final int CHUNK_SIZE = 4 * 1024 * 1024; // 每次读取 4MB
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[CHUNK_SIZE];

        try (FileInputStream fis = new FileInputStream(file)) {
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                digest.update(buffer, 0, bytesRead);
            }
        }

        byte[] hashBytes = digest.digest();
        StringBuilder hexString = new StringBuilder();

        for (byte b : hashBytes) {
            String hex = String.format("%02x", b);
            hexString.append(hex);
        }

        return hexString.toString();
    }

    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\test.las");
        try {
            String md5 = calculateFileMD5(file);
            System.out.println("文件的 MD5 值为: " + md5);
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
        }
    }
}